package com.sunriz.motordrive.controller.common;

import com.sunriz.motordrive.domain.DTO.SetValueDTO;
import com.sunriz.motordrive.domain.DTO.Result;
import com.sunriz.motordrive.server.common.SerialPortServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * ClassName: SerialPortConrtroller
 * Package: com.sunriz.motordrive.controller
 * Description:
 *
 * @Author BuTang
 * @Create 2024/4/29 15:08
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/serialPort")
public class SerialPortController {
    @Resource
    SerialPortServer serialPortServer;

    private static int RATE_115200 = 115200;
    private static int RATE_921600 = 921600;


    // todo 添加slaveId
    @GetMapping("/enable/motor/{port}")
    public Result motorEnableSerialPort(@PathVariable String port){
        return serialPortServer.motorEnableSerialPort(port,RATE_115200);
    }

    // todo 添加slaveId
    @CrossOrigin
    @GetMapping("/enable/photoThermal/hc/{port}")
    public Result hcEnableSerialPort(@PathVariable String port){
        return serialPortServer.photoThermalHcEnableSerialPort(port,RATE_115200);
    }

    @CrossOrigin
    @PutMapping("/enable/photoVoltaic/hc")
    public Result photoVoltaicHcEnableSerialPort(@RequestParam String port, @RequestParam String slaveId){
        return serialPortServer.photoVoltaicHcEnableSerialPort(port,slaveId ,RATE_115200);
    }
    @CrossOrigin
    @GetMapping("/connectStatus")
    public Result connectStatus(){
        return serialPortServer.connectStatus();
    }





    @GetMapping("/disable")
    public Result disableSerialPort(){
        return serialPortServer.disableSerialPort();
    }


    @CrossOrigin
    @PostMapping("/setValue")
    public Result setValue(@RequestBody SetValueDTO setValueDTO){
        return serialPortServer.setValue(setValueDTO);
    }
    @CrossOrigin
    @PostMapping("/check")
    public Result check(@RequestBody SetValueDTO setValueDTO){
        return serialPortServer.check(setValueDTO);
    }

    @CrossOrigin
    @GetMapping("/getPorts")
    public Result getPorts(){
        return serialPortServer.getPorts();
    }

    @DeleteMapping("/clearData")
    public Result clearData(){
        return serialPortServer.clearData();
    }


    /**
     * 驱动的串口启动
     * @param port 串口名
     * @return 返回前端结果
     */
    @GetMapping("/enable/driver")
    public Result driverEnableSerialPort(@RequestParam String port){
        Result result = serialPortServer.motorEnableSerialPort(port, RATE_115200);
        return result;
    }

}
